package swd.basic.table_name_maintain;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
   QueryBean gqbnDAO=null;
   public String action(javax.servlet.jsp.PageContext pageContext){
      HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
      HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();

      View view=(View)request.getAttribute("view");
      (new WebFunction()).setPortal_to_session(request); //set session from portal
      if (request.getSession().getAttribute("u_id")==null) {
         try{
            request.getRequestDispatcher((new StringBuffer("login.jsp")).insert(0,"/").toString()).forward(request,response);
            return "login";
         }
         catch(Exception le){
            view.setOnloadScript("can not redirect to login page...please contact IT");
            return "";
         }
      }
      //String lstrUserNO = request.getSession().getAttribute("u_id").toString();
      gqbnDAO=new QueryBean(view.getDBAlias(),view.getDBDebug(),view.getSourceCharset(),view.getTargetCharset());

      DataView DataView=null;
      DataViewForm DataViewForm=null;

      DataView = new DataView(view.getDBAlias(),view.getDBDebug(), view.getSourceCharset(), view.getTargetCharset());
      DataViewForm = new DataViewForm();

      request.setAttribute("DataView",DataView);//讓前端可以呼叫dataview
      request.setAttribute("DataViewForm",DataViewForm);


      Business business=new Business(gqbnDAO);
      //Flow flow=new Flow(gqbnDAO,lstrUserNO,"SWD",view,business);

      //String lstrResult=null;

      try{


          if(view.getdbf_no()!=null && view.getdbf_no().trim().length()>0){
             view.setSelectShow("");
             view.setInsertShow("");
          } else {
            view.setSelectShow("none");
            view.setInsertShow("");

          }



          view.setButtonVisible("0");

          if(view.getPrgAction()!=null && view.getPrgAction().equals("add")){
             view.setTipMessage("請輸入檔案編號、系統編號、檔案名稱後在於上方列按【存檔】即可");
             view.setAllClear();
             //flow.preAddAction(request.getSession().getAttribute("u_id").toString());
             view.setPrgStatus("add");
             view.setInsertShow("");
             view.setButtonVisible("1");

          }
          if(view.getPrgAction()!=null && view.getPrgAction().equals("query")){
            view.setTipMessage("若欲修改資料，請於下方修改後按【整批存檔】;若欲刪除資料，則在查詢資料列按刪除按鈕");
            view.setButtonVisible("3");
         }


          if(view.getPrgAction()!=null && view.getPrgAction().equals("save") && view.getPrgStatus().equals("add")){
             if (business.checkHaveTableData(view).equals("0")){
                //view.setOnloadScript("alert('" + flow.addAction() + "');");
                view.setTipMessage("若欲修改資料，請於下方修改後按【整批存檔】;若欲刪除資料，則在查詢資料列按刪除按鈕");
                view.setdbf_no(view.getdbf_no());
                view.setSelectShow("");
                view.setButtonVisible("2");
                view.setInsertShow("");
             } else {
                view.setOnloadScript("alert('檔案編號已存在!!');");
                view.setPrgStatus("add");
                view.setButtonVisible("1");
                view.setInsertShow("");
                view.setdbf_no(view.getdbf_no());
                view.setSelectShow("");
             }
          }


          //按整批存檔
          if(view.getPrgAction()!=null && view.getPrgAction().equals("saveall")){
              if(view.getrowsn()==null || view.getrowsn().equals("")){
                  view.setOnloadScript("alert('沒有修改!');");
              } else{
                   view.setOnloadScript("alert('"+business.batchUpdateData(view)+"');");
              }
              view.setButtonVisible("2");
              view.setPrgStatus("save");
          }

          //按刪除
          if(view.getPrgAction()!=null && view.getPrgAction().equals("delete")){
             business.exeDelSQL(view,Integer.parseInt(view.getrowsn()));
             view.setAllClear();
             view.setrowsn("9999");

          }

          // 離開
          if(view.getPrgAction()!=null && view.getPrgAction().equals("exit")){
              if (view.getPrgStatus()!=null && view.getPrgStatus().equals("add")){
                  view.setAllClear();
                  view.setButtonVisible("0");
                  view.setPrgStatus("exit");

              } else {
                 response.sendRedirect("table_name_maintain.jsp");

              }
          }

          business.setDataViewForm(DataViewForm,view,request.getSession().getAttribute("u_id").toString());




         return "";
      }
      catch(Exception e){
         view.setOnloadScript("alert('Program error please contact IT.');");
         return "";
      }
      finally{
         try{
            if(gqbnDAO!=null){
               gqbnDAO.close();
            }
         }
         catch(Exception e){
            System.out.println("can not close querybean in Action.java");
         }
      }
   }
}
